package com.hejing.手动抛出异常_throw;

/**
 * ClassName: ThrowTest
 * Package: com.hejing.手动抛出异常
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-04-11 20:38
 * @Version: 1.0
 */
public class ThrowTest {
    public static void main(String[] args) {
        Student s1 = new Student();
        try {
            s1.register(-10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Student {
    int id;

    public void register(int id) throws Exception {
        if (id > 0) {
            this.id = id;
        } else {
            // 手动抛出异常类对象

            // 抛出运行时异常
            // throw new RuntimeException("输入的id非法");

            // 抛出Exception, 此时必须进行异常处理
            // throw new Exception("输入的id非法");

            // 抛出自定义异常
            throw new BelowZeroException();
        }
    }
}





























